//
//  KABillListHeader.swift
//  kano
//
//  Created by Frank on 2024/9/18.
//

import Foundation
import UIKit

class KABillListHeader: UIView {
    
    lazy var var_billListTip: KABillListTip = {
        var_billListTip = KABillListTip()
        return var_billListTip
    }()
    
    lazy var var_bgView: UIImageView = {
        let var_bgView = UIImageView(image: UIImage(named: "billList_bg_blue"))
        var_bgView.isUserInteractionEnabled = true
        return var_bgView
    }()
    
    
    public var monthPayLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: kscale(22), weight: .bold)
        label.textColor = .white
        label.text = "₦ 0"
        return label
    }()
    
    public var amountTitle: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: kscale(12), weight: .medium)
        label.textColor = .white
        label.text = "Repayment amount"
        return label
    }()
    
    public var amountLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: kscale(12), weight: .medium)
        label.textColor = .white
        label.text = ""
        return label
    }()
    
    public var unpaidTitle: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: kscale(11), weight: .regular)
        label.textColor = .colorD5D5F9
        label.text = "No of unpaid loans"
        return label
    }()
    
    public var unpaidLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: kscale(11), weight: .medium)
        label.textColor = .white
        label.text = "0"
        return label
    }()
    
    lazy var var_overdueTitle: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: kscale(11), weight: .regular)
        label.textColor = .colorD5D5F9
        label.text = "Overdue loans"
        return label
    }()
    
    public var var_overdueLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: kscale(11), weight: .medium)
        label.textColor = .white
        label.text = "0"
        return label
    }()
    
    lazy var var_historyBtn: UIButton = {
        let button = UIButton()
        button.setTitle(" History > ", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: kscale(11), weight: .medium)
        //        button.setImage(UIImage(named: "repay_sel"), for: .selected)
        button.addTarget(self, action: #selector(historyBtnClick(_:)), for: .touchUpInside)
        return button
    }()
    
    @objc func historyBtnClick(_ sender: UIButton) {
        sender.preventDoubleTap()
        let btnInfo = KABtnInfo(eleId: "BillToLoanList_Nav", pageId: "BillStatement_Overview", clickTime: Date.ka_currentData())
        KAReport.ka_reportWith(type: .btn, data: btnInfo.kj.JSONObject())
        
        KAFlutterManager.showFlutterVC(route: "/historyList", parameter: ["mobile" : KAUserManager.shared.user.notificationMessage])
    }
    
    
    var overdueLoans = 0
    var model: KABillListModel? {
        didSet {
            if model!.accessTemporaryToken?.count ?? 0 > 0 {
                model!.accessTemporaryToken?.map({ loan in
                    if loan.productClassification == 2 {
                        loan.credentialData?.map({ subLoan in
                            if subLoan.overdueDuration > 0 {
                                overdueLoans += 1
                            }
                        })
                    }
                    if loan.productClassification == 1 && loan.overdueDuration > 0 {
                        overdueLoans += 1
                    }
                })
                monthPayLabel.text = "₦ \(model!.outstandingRepaymentTotal.separatorString())"
                unpaidLabel.text = "\(model!.invitationRollData)"
                var_overdueLabel.text = "\(overdueLoans)"
            }
        }
    }
    //    override init(reuseIdentifier: String?) {
    //        super.init(reuseIdentifier: reuseIdentifier)
    //        contentView.backgroundColor = .white
    //        ka_setUI()
    //    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        ka_setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func ka_setUI() {
        backgroundColor = .white
        addSubview(var_billListTip)
        addSubview(var_bgView)
        var_bgView.addSubview(monthPayLabel)
        var_bgView.addSubview(amountTitle)
        var_bgView.addSubview(amountLabel)
        var_bgView.addSubview(unpaidTitle)
        var_bgView.addSubview(unpaidLabel)
        var_bgView.addSubview(var_overdueTitle)
        var_bgView.addSubview(var_overdueLabel)
        
        var_bgView.addSubview(var_historyBtn)
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        var_bgView.snp.makeConstraints { make in
            make.left.equalTo(kscale(12));
            make.right.equalTo(kscale(-12));
            make.top.equalTo(var_billListTip.snp.bottom).offset(12);
            make.height.equalTo(kscale(152))
        }
        
        monthPayLabel.snp.makeConstraints { make in
            make.left.equalTo(kscale(16))
            make.top.equalTo(var_bgView.snp.top).offset(kscale(26))
        }
        
        amountTitle.snp.makeConstraints { make in
            make.left.equalTo(kscale(16))
            //            make.top.equalTo(monthPayLabel.snp.bottom).offset(kscale(4))
            make.top.equalTo(var_bgView.snp.top).offset(kscale(64))
        }
        amountLabel.snp.makeConstraints { make in
            make.centerY.equalTo(amountTitle)
            make.left.equalTo(amountTitle.snp.right).offset(kscale(8))
        }
        unpaidTitle.snp.makeConstraints { make in
            make.left.equalTo(kscale(16))
            make.bottom.equalTo(var_bgView.snp.bottom).offset(kscale(-34.5))
        }
        unpaidLabel.snp.makeConstraints { make in
            make.left.equalTo(unpaidTitle.snp.right).offset(kscale(8))
            make.top.equalTo(unpaidTitle)
        }
        var_overdueTitle.snp.makeConstraints { make in
            make.left.equalTo(unpaidLabel.snp.right).offset(kscale(32))
            make.top.equalTo(unpaidTitle)
        }
        var_overdueLabel.snp.makeConstraints { make in
            make.left.equalTo(var_overdueTitle.snp.right).offset(kscale(8))
            make.top.equalTo(unpaidTitle)
        }
        
        var_historyBtn.snp.makeConstraints { make in
            make.left.equalTo(monthPayLabel.snp.right).offset(kscale(8))
            make.centerY.equalTo(monthPayLabel)
            //            make.top.equalTo(var_bgView.snp.top).offset(kscale(26))
            make.height.equalTo(kscale(20))
        }
        
        var_historyBtn.layer.cornerRadius = 4
        var_historyBtn.layer.masksToBounds = false
        var_historyBtn.layer.borderColor = UIColor.white.cgColor
        var_historyBtn.layer.borderWidth = 1
    }
    
}
